﻿using System;
using System.IO;
using System.IO.Packaging;
using System.Text;
using System.Xml;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;

namespace Vision.Core.ActivityComponents.Office
{
    /// <summary>
    /// 
    /// </summary>
    public class CreateWordDocument : CodeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> FileName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> BodyText { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(CodeActivityContext context)
        {
            SaveDOCX(context.GetValue(this.FileName), context.GetValue(this.BodyText));    
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="BodyText"></param>
        private void SaveDOCX(string fileName, string BodyText)
        {
            // use the Open XML namespace for WordprocessingML:
            string WordprocessingML = "http://schemas.openxmlformats.org/wordprocessingml/2006/3/main";

            // create the start part, set up the nested structure ...
            XmlDocument xmlStartPart = new XmlDocument();
            XmlElement tagDocument = xmlStartPart.CreateElement("w:document", WordprocessingML);
            xmlStartPart.AppendChild(tagDocument);
            XmlElement tagBody = xmlStartPart.CreateElement("w:body", WordprocessingML);
            tagDocument.AppendChild(tagBody);
            XmlElement tagParagraph = xmlStartPart.CreateElement("w:p", WordprocessingML);
            tagBody.AppendChild(tagParagraph);
            XmlElement tagRun = xmlStartPart.CreateElement("w:r", WordprocessingML);
            tagParagraph.AppendChild(tagRun);
            XmlElement tagText = xmlStartPart.CreateElement("w:t", WordprocessingML);
            tagRun.AppendChild(tagText);

            // Note nesting of tags for the WordprocessingML document (the "start part") ...
            // w:document contains ...
            //     w:body, which contains ...
            //         w:p (paragraph), which contains ...
            //             w:r (run), which contains ...
            //                 w:t (text)

            // insert text into the start part, as a "Text" node ...
            XmlNode nodeText = xmlStartPart.CreateNode(XmlNodeType.Text, "w:t", WordprocessingML);
            nodeText.Value = BodyText;
            tagText.AppendChild(nodeText);

            // create a new package (Open XML document) ...
            Package pkgOutputDoc = null;
            pkgOutputDoc = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite);

            // save the main document part (document.xml) ...
            Uri uri = new Uri("/word/document.xml", UriKind.Relative);
            PackagePart partDocumentXML = pkgOutputDoc.CreatePart(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
            StreamWriter streamStartPart = new StreamWriter(partDocumentXML.GetStream(FileMode.Create, FileAccess.Write));
            xmlStartPart.Save(streamStartPart);
            streamStartPart.Close();
            pkgOutputDoc.Flush();

            // create the relationship part, close the document ...
            pkgOutputDoc.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId1");
            pkgOutputDoc.Flush();
            pkgOutputDoc.Close();
        }
    }
}
